package com.ct.gd.passport.common;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.LoaderManager;

import com.umeng.analytics.MobclickAgent;
import com.up91.common.android.App;
import com.up91.common.android.async.ILoading;
import com.up91.common.android.async.Loading;

public abstract class BaseActivity extends FragmentActivity implements ILoading {
	private Loading			mLoading;
	private FragmentManager	mFgMan;
	private LoaderManager	mLoaderMan;

	@Override
	final protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
//		MobclickAgent.onError(this);
		CTApp app = (CTApp) getApplication();
		app.registActivity(this);

		mFgMan = getSupportFragmentManager();
		mLoaderMan = getSupportLoaderManager();
		mLoading = initLoading();

		onBeforeCreate(savedInstanceState);
		onBaseCreate(savedInstanceState);
		initViews();
		setFieldValues();
		onAfterCreate(savedInstanceState);
	}
	
	@Override
	protected void onResume() {
	    super.onResume();
	    MobclickAgent.onResume(this);
	}
	
	@Override
	protected void onPause() {
	    super.onPause();
	    MobclickAgent.onPause(this);
	}

	@Override
	public Loading initLoading() {
		return new Loading(this);
	}

	@Override
	public Loading getLoading() {
		return mLoading;
	}

	@Override
	public Context getLoadingContext() {
		return this;
	}

	public FragmentManager getFragmentManager() {
		return mFgMan;
	}

	public LoaderManager getLoaderManager() {
		return mLoaderMan;
	}

	/**
	 * 基础构建前的动作，需要时进行重载
	 * 
	 * @param savedInstanceState bundle
	 */
	protected void onBeforeCreate(Bundle savedInstanceState) {
	}

	/**
	 * 基础构建动作，常用于设置视图等相关动作
	 * 
	 * @param savedInstanceState bundle
	 */
	abstract protected void onBaseCreate(Bundle savedInstanceState);

	/**
	 * 控件初始化后相关动作，可以初始化需要的Handler
	 * 
	 * @param savedInstanceStat bundle
	 */
	protected void onAfterCreate(Bundle savedInstanceStat) {
	};

	/**
	 * 控件初始化
	 */
	abstract protected void initViews();

	/**
	 * 成员初始化
	 */
	protected void setFieldValues() {
	}

	public void showExitDialog() {
		AlertDialog dialog = new AlertDialog.Builder(this).setTitle("确定要退出程序吗?")
				.setNeutralButton("确定", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						((App) App.getApplication()).exit();
					}
				}).setNegativeButton("取消", null).create();
		dialog.show();
	}
}
